package com.wanxi.service.impl;


import com.wanxi.feign.PatientClient;
import com.wanxi.mapper.PatientMapper;
import com.wanxi.model.PatientApplyQuery;
import com.wanxi.model.PatientQuery;
import com.wanxi.entity.Patient;
import com.wanxi.entity.PatientApply;
import com.wanxi.result.CommonResult;
import com.wanxi.vo.PatientApplyDTO;
import com.wanxi.vo.PatientDTO;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "患者服务")
@RestController
@RequestMapping("/patient")
public class PatientServiceImpl implements PatientClient {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private PatientMapper patientMapper;

    @GetMapping("/findPatient")
    @Override
    public CommonResult findPatient(PatientQuery patientQuery) {
        if(patientQuery.getDoctorId() != null){
            logger.info("入参",patientQuery);
            List<PatientDTO> patients = patientMapper.findPatientByGuardianId(patientQuery);
            logger.info("出参",patients);
            return CommonResult.success(patients);
        }
        logger.info("入参",patientQuery);
        List<PatientDTO> patients = patientMapper.findPatient(patientQuery);
        logger.info("出参",patients);
        return CommonResult.success(patients);
    }

    @GetMapping("/findPatientBack")
    @Override
    public CommonResult findPatientBack(PatientQuery patientQuery) {
        if(patientQuery.getDoctorId()== null){
            logger.info("入参",patientQuery);
            List<Patient> patients = patientMapper.findPatientBackByGuardianId(patientQuery);
            logger.info("出参",patients);
            return CommonResult.success(patients);
        }
        logger.info("入参",patientQuery);
        List<Patient> patients = patientMapper.findPatientBack(patientQuery);
        logger.info("出参",patients);
        return CommonResult.success(patients);
    }

    @PostMapping("/addPatient")
    @Override
    public CommonResult addPatient(PatientDTO patientDTO) {
        logger.info("入参",patientDTO);
        Integer i = patientMapper.addPatient(patientDTO);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }
    @PutMapping("/updatePatient")
    @Override
    public CommonResult updatePatient(Long patientId, PatientDTO patientDTO) {
        logger.info("入参",patientDTO);
        Integer i = patientMapper.updatePatient(patientId, patientDTO);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    @PutMapping("/updatePatientState")
    @Override
    public CommonResult updatePatientState(Long id, Integer state) {
        logger.info("入参",id,state);
        Integer i = patientMapper.updatePatientState(id, state);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @GetMapping("/getPatientApply")
    @Override
    public CommonResult getPatientApply(PatientApplyQuery patientApplyQuery) {
        logger.info("入参",patientApplyQuery);
        List<PatientApply> patientApplys = patientMapper.getPatientApply(patientApplyQuery);
        logger.info("出参",patientApplys);
        return CommonResult.success(patientApplys);
    }

    @GetMapping("/getPatientApplyBack")
    @Override
    public CommonResult getPatientApplyBack(PatientApplyQuery patientApplyQuery) {
        logger.info("入参",patientApplyQuery);
        List<PatientApply> patientApplyBacks = patientMapper.getPatientApplyBack(patientApplyQuery);
        logger.info("出参",patientApplyBacks);
        return CommonResult.success(patientApplyBacks);
    }

    @PostMapping("/addPatientApply")
    @Override
    public CommonResult addPatientApply(PatientApplyDTO patientApplyDTO) {
        logger.info("入参",patientApplyDTO);
        Integer i = patientMapper.addPatientApply(patientApplyDTO);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    @PutMapping("/updatePatientApply")
    @Override
    public CommonResult updatePatientApply(Long patientApplyId, PatientApplyDTO patientApplyDTO) {
        logger.info("入参",patientApplyId,patientApplyDTO);
        Integer i = patientMapper.updatePatientApply(patientApplyId, patientApplyDTO);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    @PutMapping("/updatePatientApplyState")
    @Override
    public CommonResult updatePatientApplyState(Long patientApplyId, Integer state) {
        logger.info("入参",patientApplyId,state);
        Integer i = patientMapper.updatePatientApplyState(patientApplyId, state);
        logger.info("出参",i);
        if (i > 0){
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }
}
